<?php

class Involved_NewsModel extends BaseModel
{

    protected $table = 'news';

    /**
     * 文章列表
     * @return mixed
     */
    public function NewsSelect()
    {

        return DB::table($this->table)
            ->select('news.auto_id','summary','title','cate_id','admin_id','news.add_time','thumb_url','is_valid','user_nick','news_category.name','media_logo','is_carousel')
            ->LeftJoin('admin_user','admin_user.user_id','=','news.admin_id')
            ->LeftJoin('news_category','news_category.auto_id','=','news.cate_id')
            ->orderBy('news.add_time', 'desc')
            ->get();
    }

    /**
     * 首页公告列表
     * @return mixed
     */
    public function NoticeList()
    {
        return DB::table($this->table)
            ->select('news.auto_id','title','cate_id','summary','news.add_time','thumb_url','is_valid','news_category.name','media_logo','is_carousel')
            ->where('cate_id','2')
            ->where('is_valid','1')
            ->LeftJoin('news_category','news_category.auto_id','=','news.cate_id')
            ->orderBy('news.add_time', 'desc')
            ->take(5)
            ->get();
    }

    /**
     * 公告/媒体列表
     * @return mixed
     */
    public function OtherNewsList($type)
    {
        return DB::table($this->table)
            ->select('auto_id','title','cate_id','summary','add_time','thumb_url','is_valid','media_logo','is_carousel')
            ->where('cate_id',$type)
            ->where('is_valid','1')
            ->orderBy('add_time', 'desc')
            ->paginate(10);
    }

    /**
     * 公告/媒体详细
     * @return mixed
     */
    public function NewsDetailed($id)
    {
        return DB::table($this->table)
            ->select('auto_id','title','cate_id','summary','content','add_time','thumb_url','is_valid','media_logo','is_carousel')
            ->where('auto_id',$id)
            ->first();
    }

    /**
     * 首页媒体列表
     * @return mixed
     */
    public function IndexNewsList()
    {
        return DB::table($this->table)
            ->select('news.auto_id','title','cate_id','news.add_time','thumb_url','is_valid','news_category.name','media_logo','is_carousel')
            ->where('cate_id','1')
            ->where('is_valid','1')
            ->LeftJoin('news_category','news_category.auto_id','=','news.cate_id')
            ->orderBy('news.add_time', 'desc')
            ->take(5)
            ->get();
    }



    /**
     * 修改获取信息
     * @param $auto_id
     * @return mixed
     */
    public function GetNewsId($auto_id)
    {
        return DB::table($this->table)
            ->select('auto_id','summary','title','cate_id','admin_id','add_time','thumb_url','is_valid','content','media_logo','is_carousel','media_source','link')
            ->where('auto_id',$auto_id)
            ->first();
    }

    /**
     * 文章详情ajax返回
     * @param $auto_id
     * @return mixed
     */
    public function NewsDetails($auto_id)
    {
        $news_list =  DB::table($this->table)
            ->select('news.auto_id','title','user_nick','news_category.name','summary','news.content','news.add_time','is_valid','thumb_url','media_logo','is_carousel','media_source','link')
            ->LeftJoin('admin_user','admin_user.user_id','=','news.admin_id')
            ->LeftJoin('news_category','news_category.auto_id','=','news.cate_id')
            ->where('news.auto_id',$auto_id)
            ->first();

        $news_explain = [
            'auto_id'                => '文章ID',
            'title'                  => '文章标题',
            'user_nick'              => '文章添加的管理员',
            'name'                   => '文章分类',
            'summary'                => '文章摘要',
            'content'                => '文章内容',
            'add_time'               => '文章添加时间',
            'is_valid'               => '文章是否有效',
            'thumb_url'              => '图片',
            'media_logo'             => '媒体公告logo',
            'is_carousel'            => '是否启用轮播图',
            'media_source'           => '媒体来源',
            'link'                   => '文章链接',


        ];

        return $data =[
            'news_list'    =>$news_list,
            'news_explain' =>$news_explain,
        ];

    }

    /**
     * 分类显示
     * @param $cate_id
     * @return mixed
     */
    public function NewsCateShow($cate_id)
    {
        return DB::table($this->table)
            ->select('news.auto_id','summary','title','cate_id','admin_id','news.add_time','thumb_url','is_valid','user_nick','news_category.name','media_logo','is_carousel')
            ->LeftJoin('admin_user','admin_user.user_id','=','news.admin_id')
            ->LeftJoin('news_category','news_category.auto_id','=','news.cate_id')
            ->where('cate_id',$cate_id)
            ->orderBy('news.add_time', 'desc')
            ->get();
    }


    /**
     * API我的公告
     * @param $auto_id
     * @return mixed
     */
    public function ApiNewsId($page)
    {
        $offset = ($page-1) * HostEnum::PAGE_NUM;
        return DB::table($this->table)
            ->select('auto_id','title','add_time','is_valid')
            ->where('is_valid',1)
            ->skip($offset)->take(HostEnum::PAGE_NUM)
            ->orderBy('add_time', 'desc')
            ->get();
    }


}
